﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.Web
{
    public static class MyRequest
    {
        public static int? GetIntNullable(this System.Web.HttpRequestBase request, string keyName)
        {
            return GetIntNullable(request, keyName, null);
        }

        public static int? GetIntNullable(this System.Web.HttpRequestBase request, string keyName, int? defaultValue)
        {
            return request[keyName].ToIntNullable(defaultValue);
        }

        public static string GetString(this System.Web.HttpRequestBase request, string name, string defaultValue = "")
        {
            try
            {
                var s = request[name];

                if (string.IsNullOrWhiteSpace(s))
                {
                    return defaultValue;
                }
                else
                {
                    return HttpUtility.UrlDecode(s).Trim();
                }
            }
            catch (Exception)
            {
                return defaultValue;
            }


        }

        public static bool GetBool(this System.Web.HttpRequestBase request, string name, bool defaultValue = false)
        {
            try
            {
                var s = request[name];

                if (string.IsNullOrWhiteSpace(s))
                {
                    return defaultValue;
                }
                else
                {
                    var s1 = HttpUtility.UrlDecode(s).Trim();

                    if (string.Compare(s1, "true", true) == 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return defaultValue;
            }


        }

        public static int GetInt(this System.Web.HttpRequestBase request, string name, int defaultValue = 0)
        {
            var s = GetString(request, name);

            var i = 0;

            if (int.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return defaultValue;
            }
        }

        public static decimal GetDecimal(this System.Web.HttpRequestBase request, string name, decimal defaultValue = 0m)
        {
            var s = GetString(request, name);

            decimal i;

            if (decimal.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return defaultValue;
            }
        }

        public static long GetLong(this HttpRequestBase request, string name, long defaultValue = 0)
        {
            var s = GetString(request, name);

            long i = 0;

            if (long.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return defaultValue;
            }
        }

        public static DateTime? GetDateTimeNullable(this HttpRequestBase request, string name, string formatStr,
            DateTime? defaultValue)
        {
            var s = GetString(request, name);

            long i = 0;

            return s.ToDateTimeNullable(formatStr, defaultValue);
        }

        public static DateTime GetDateTime(this HttpRequestBase request, string name, string formatStr,
            DateTime defaultValue)
        {
            var s = GetString(request, name);

            long i = 0;

            return s.ToDateTime(formatStr, defaultValue);
        }


        public static string GetString(this System.Web.HttpRequest request, string name,string defaultValue="")
        {
            try
            {
                var s = request[name];

                if (string.IsNullOrWhiteSpace(s))
                {
                    return defaultValue;
                }
                else
                {
                    return HttpUtility.UrlDecode(s).Trim();
                }
            }
            catch (Exception)
            {
                return defaultValue;
            }

            
        }

        public static bool GetBool(this System.Web.HttpRequest request, string name, bool defaultValue = false)
        {
            try
            {
                var s = request[name];

                if (string.IsNullOrWhiteSpace(s))
                {
                    return defaultValue;
                }
                else
                {
                    var s1= HttpUtility.UrlDecode(s).Trim();

                    if (string.Compare(s1,"true",true)==0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return defaultValue;
            }


        }

        public static int GetInt(this System.Web.HttpRequest request, string name, int defaultValue = 0)
        {
            var s = GetString(request, name);

            var i = 0;

            if (int.TryParse(s,out i))
            {
                return i;
            }
            else
            {
                return defaultValue;
            }
        }

        public static decimal GetDecimal(this System.Web.HttpRequest request, string name, decimal defaultValue = 0m)
        {
            var s = GetString(request, name);

            decimal i ;

            if (decimal.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return defaultValue;
            }
        }

        public static long GetLong(this HttpRequest request, string name, long defaultValue = 0)
        {
            var s = GetString(request, name);

            long i = 0;

            if (long.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return defaultValue;
            }
        }

        public static DateTime? GetDateTimeNullable(this HttpRequest request, string name, string formatStr,
                                                    DateTime? defaultValue)
        {
            var s = GetString(request, name);

            long i = 0;

            return s.ToDateTimeNullable(formatStr, defaultValue);
        }

        public static DateTime GetDateTime(this HttpRequest request, string name,string formatStr, DateTime defaultValue)
        {
            var s = GetString(request, name);

            long i = 0;

            return s.ToDateTime(formatStr,defaultValue);

            //if (long.TryParse(s, out i))
            //{
            //    return i;
            //}
            //else
            //{
            //    return defaultValue;
            //}
        }
    
    }
}
